using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TreeSharp;

using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.BehaviorTree;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace FelMaster.Classes.Shaman
{
    class Elemental : TalentBase
    {
        public override string Name
        {
            get { return "Elemental Shaman V0.1"; }
        }

        public override string KeySpell
        {
            get { return "Thunderstorm"; }
        }
        
        private static bool ChkTotem(string totem)
        {
            bool totchk = false;
            foreach (Styx.WoWInternals.WoWObjects.WoWTotemInfo ti in Me.Totems)
            {
                if (ti.Name == totem)
                    totchk = true;
            }
            return totchk;
        }
        
        public List<WoWUnit> AOECHK(WoWPoint chkloc, double radius)
        {
            List<WoWUnit> hostile = ObjectManager.GetObjectsOfType<WoWUnit>(true, false);
            List<WoWUnit> avoid = new List<WoWUnit>();
            var MaxDistance2 = radius * radius;
            try
            {
                hostile = hostile.Where(x =>
                    x.IsAlive &&
                    (x.IsTargetingMeOrPet || x.IsTargetingMyPartyMember || x.IsTargetingMyRaidMember || x.IsTargetingPet) &&
                    !x.IsCritter &&
                    !x.IsPet &&
                    x.Location.Distance2DSqr(chkloc) < MaxDistance2
                ).ToList();
                avoid = hostile.Where(x =>
                    //check for controlled units, like sheep etc
                SimCraftBase.Instance.UnitIsControlled(x, true)
                ).ToList();
                if (avoid != null && avoid.Count > 0)
                {
                    hostile = null;
                    hostile.Add(Me.CurrentTarget);
                }
            }
            catch { }
            return hostile;
        }
        
        public override Composite SingleRotation
        {
            get {
                return new PrioritySelector(
                            new Decorator(x => SC.PlayerIsChanneling, new TreeSharp.Action(a => SC.Log("Channeling"))),
                            SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                            SC.CastSpell("Blood Fury", a => true, "Blood Fury"),
                            SC.CastSpell("Lifeblood", a => true, "Lifeblood"),
                            SC.CastSpell("Berserking", a => true, "Berserking"),
                            SC.CastSpell("Elemental Mastery", a => true, "Elemental Mastery"),
                            SC.CastSpell("Unleash Elements", a => Me.IsMoving, "Unleash Elements"),
                            SC.CastDebuff("Flame Shock", a => true, "Flame Shock"),
                            SC.CastSpell("Lava Burst", a => SC.TargetDebuffTimeLeft("Flame Shock") > 2.5, "Lava Burst"),
                            SC.CastSpell("Earth shock", a => SC.PlayerCountBuff("Lightning Shield") == 9, "Earth shock"),
                            SC.CastSpell("Earth shock", a => SC.PlayerCountBuff("Lightning Shield") > 6 && SC.TargetDebuffTimeLeft("Flame Shock") > 7, "Earth shock"),
                            SC.CastSpell("Searing Totem", a => !(ChkTotem("Searing Totem") || ChkTotem("Fire Elemental Totem")), "Searing Totem"),
                            SC.CastSpell("Spiritwalker's Grace", a => Me.IsMoving, "Spiritwalker's Grace"),
                            SC.CastSpell("Chain Lightning", a => Me.GotTarget && AOECHK(Me.CurrentTarget.Location, 8).Count >= 2, "Chain Lightning"),
                            SC.CastSpell("Lightning Bolt", a => true, "Lightning Bolt"),
                            SC.CastSpell("Thunderstorm", a => true, "Thunderstorm")
                );
            }
        }

        public override Composite Buffs
        {
            get
            {
                return new PrioritySelector(
                    SC.CastBuff("Lightning Shield", a => true, "Lightning Shield")
                );
            }
        }

        public override Composite PVPRotation
        {
            get { throw new NotImplementedException(); }
        }

        public override Composite PVERotation
        {
            get
            {
                return SingleRotation;
            }
        }
    }
}